from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from param.Parameter import Parameter
import sys


class ParamMain(QWidget):

    updated_ip_port = pyqtSignal()

    def __init__(self):
        super(ParamMain, self).__init__()
        self.parameter = Parameter()

        self.setWindowTitle("设置默认的IP和端口号")

        self.lbl_ip = QLabel(self)
        self.lbl_ip.setText("IP")
        self.lbl_port = QLabel(self)
        self.lbl_port.setText("Port")

        self.lineEdit_ip = QLineEdit(self)
        self.lineEdit_ip.setText(self.parameter.getIP())
        self.lineEdit_port = QLineEdit(self)
        self.lineEdit_port.setText(self.parameter.getPort())

        self.btn_finish = QPushButton(self)
        self.btn_finish.setText("确定")
        self.btn_finish.clicked.connect(self.finish)
        self.btn_cancel = QPushButton(self)
        self.btn_cancel.setText("取消")
        self.btn_cancel.clicked.connect(self.cancel)

        self.grid_layout = QGridLayout()
        self.grid_layout.addWidget(self.lbl_ip, 0, 0)
        self.grid_layout.addWidget(self.lineEdit_ip, 0, 1)
        self.grid_layout.addWidget(self.lbl_port, 1, 0)
        self.grid_layout.addWidget(self.lineEdit_port, 1, 1)
        self.grid_layout.addWidget(self.btn_cancel, 2, 0)
        self.grid_layout.addWidget(self.btn_finish, 2, 1)
        self.setLayout(self.grid_layout)


    def finish(self):
        self.parameter.setIP(self.lineEdit_ip.text())
        self.parameter.setPort(self.lineEdit_port.text())
        self.updated_ip_port.emit()
        self.close()

    def cancel(self):
        self.close()

if __name__ == '__main__':
    app = QApplication(sys.argv)
    demo = ParamMain()
    demo.show()
    sys.exit(app.exec_())